package com.motorhome.h5.common.interceptor;

import com.motorhome.base.cache.MemcachedCache;
import com.motorhome.base.session.CookieSession;
import com.motorhome.base.util.StringUtil;
import com.motorhome.h5.common.statics.H5Statics;
import com.motorhome.h5.domain.User;
import com.motorhome.h5.service.CommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author: John
 */
public class H5ValidInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(H5ValidInterceptor.class);

    @Autowired
    private MemcachedCache cache;

    @Autowired
    private CommonService commonService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {

        Map<String, String[]> paramsMap = request.getParameterMap();
        for (String key : paramsMap.keySet()) {
            request.setAttribute(key, paramsMap.get(key)[0]);
        }

        CookieSession session = new CookieSession(request, response, H5Statics.SESSION_KEY);
        String userId = session.get("uid");
        if (StringUtil.isBlank(userId)) {

            //网站跳转过来
            if (request.getRequestURI().toLowerCase().contains("/webindex")) {
                String startDate = request.getParameter("fromdate");
                String endDate = request.getParameter("backdate");
                String days = request.getParameter("days");
                request.getSession(true).setAttribute("startCityIdWeb", "1");
                request.getSession(true).setAttribute("startCityNameWeb", "南京");
                request.getSession(true).setAttribute("startShopIdWeb", "1");
                request.getSession(true).setAttribute("startShopNameWeb", "南京秦淮河营地店");
                request.getSession(true).setAttribute("startDateWeb", startDate);
                request.getSession(true).setAttribute("endCityIdWeb", "1");
                request.getSession(true).setAttribute("endCityNameWeb", "南京");
                request.getSession(true).setAttribute("endShopIdWeb", "1");
                request.getSession(true).setAttribute("endShopNameWeb", "南京秦淮河营地店");
                request.getSession(true).setAttribute("endDateWeb", endDate);
                request.getSession(true).setAttribute("daysWeb", days);
                response.sendRedirect(request.getContextPath() + "/h5/login/login?source=web");
                return false;
            }
            response.sendRedirect(request.getContextPath() + "/h5/login/login");
            return false;
        }

        User loginUser = (User)cache.get("H5_USER_" + userId);
        if (loginUser == null) {
            loginUser = commonService.getUserById(Long.parseLong(userId));
            if (loginUser != null) {
                cache.set("H5_USER_" + userId, loginUser, "24h");
            }
        }
        request.setAttribute("loginUser", loginUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            modelAndView.addObject("baseFileUrl", H5Statics.baseFileUrl);
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {

    }
}
